var gulp = require('gulp');
var concat = require('gulp-concat');
var watch = require('gulp-watch');
var uglify = require('gulp-uglify');
var clean = require('gulp-clean');
var replace = require('gulp-replace');
var gulpCopy = require('gulp-copy');
//var livereload = require('gulp-livereload');



// 合并到tmp目录下，用于同步到java项目
gulp.task('concatScriptsAndDeploy',function(){
    return gulp.src('./js/modules/*.js')                   // 将js/modules/下所有JS文件
        .pipe(concat('common.js', {newLine: '\r\n'}))      // 以\r\n为分隔符，合并成common.js
        .pipe(gulp.dest('./tmp/'));                        // 输出到tmp/目录下暂时保存
});

// 合并到本项目
gulp.task('concatScripts',function(){
    return gulp.src('./js/modules/*.js')
        .pipe(concat('common.js', {newLine: '\r\n'}))
        .pipe(gulp.dest('./js/'));
});

gulp.task('concatCssAndDeploy',function(){
    return gulp.src('./css/modules/*.css')                 // css/modules/下所有css文件
        .pipe(concat('style.css', {newLine: '\r\n'}))      // 以\r\n为分隔符，合并成style.css
        .pipe(gulp.dest('./tmp/'));                        // 输出到tmp/目录下
});

gulp.task('concatCss',function(){
    return gulp.src('./css/modules/*.css')
        .pipe(concat('style.css', {newLine: '\r\n'}))
        .pipe(gulp.dest('./css/'));
});

gulp.task('watch',function(){
    gulp.watch('js/modules/*.js', ['jsDelTmp', 'concatScripts']);      // 监控js/modules/目录下的所有js文件，delTmp任务
    gulp.watch('css/modules/*.css', ['cssDelTmp', 'concatCss']);        // 监控css/modules/目录下的所有css文件，发生修改时执行concatCss任务
    gulp.watch('templates/*.hbs', ['copyHbs']);
});

gulp.task('compress',function(){
    return gulp.src(['js/common.js'])                       // 对js/common.js
        .pipe(uglify({                                      // 执行uglify任务（压缩JS）
            "mangle": true
        }))
        .pipe(gulp.dest('prod/js'));                        // 压缩后输出到prod/js目录下
});

// 任务执行顺序：合并common.js(concatScripts) --> 修改url保存在当前路径tmp目录下（changeUrl）--> 清空目标路径（cleanDeployPath）--> 将修改后的common.js复制到目标路径（copyScripts）--> 删除当前路径tmp目录（delTmp）
// 目标路径，如D:/messcat/workspacenew/notvsys/WebContent/js
var jsDeployPath =  'D:/svn/jiehuo_new/jiehuo/WebContent/js';
var cssDeployPath = 'D:/svn/jiehuo_new/jiehuo/WebContent/css';
var hbsDeployPath = 'D:/svn/jiehuo_new/jiehuo/WebContent/templates';

/* ---------------------- JS ---------------------- */

// 清空目标路径
gulp.task('cleanJsDeployPath', ['concatScriptsAndDeploy'], function () {
    return gulp.src(jsDeployPath + '/*', {read: false})
        .pipe(clean({force: true}));
});

// 将common.js复制到目标路径
gulp.task('copyScripts', ['cleanJsDeployPath'], function(){
    return gulp.src('./tmp/common.js')
        .pipe(gulpCopy(jsDeployPath, {'prefix': 2}));
});

// 删除tmp文件夹
gulp.task('jsDelTmp', ['copyScripts'], function () {
    return gulp.src('./tmp', {read: false})
        .pipe(clean());
});

/* ---------------------- CSS ---------------------- */

// 清空目标路径
gulp.task('cleanCssDeployPath', ['concatCssAndDeploy'], function () {
    return gulp.src(cssDeployPath + '/*', {read: false})
        .pipe(clean({force: true}));
});

// 将style.css复制到目标路径
gulp.task('copyCss', ['cleanCssDeployPath'], function(){
    return gulp.src('./tmp/style.css')
    .pipe(gulpCopy(cssDeployPath, {'prefix': 2}));
});

// 删除tmp文件夹
gulp.task('cssDelTmp', ['copyCss'], function () {
    return gulp.src('./tmp', {read: false})
        .pipe(clean());
});

/* ---------------------- HBS ---------------------- */

// 清空目标路径
gulp.task('cleanHbsDeployPath', function () {
    return gulp.src(hbsDeployPath + '/*', {read: false})
        .pipe(clean({force: true}));
});

// 将所有.hbs复制到目标路径
gulp.task('copyHbs', ['cleanHbsDeployPath'], function(){
    return gulp.src('./templates/*.hbs')
        .pipe(gulpCopy(hbsDeployPath, {'prefix': 2}));
});



//chrome 刷新
//gulp.task('watch',function(){
//    livereload.listen(); //要在这里调用listen()方法
//});